# Sandbox

The sandbox executes a set of commands to validate Sweep's changes using linters and type-checkers after every edit and fixes them when they fail. By default we use [Trunk](https://trunk.io), an opinionated super-linter that installs all the common formatters and linters for your codebase.

## Configuration

Go to your local clone of your repo. Create your `sweep.yaml` if it doesn't already exist and copy the following template into it:

<details>
<summary>Template&nbsp;`sweep.yaml`&nbsp;to copy</summary>

```yml
# Sweep AI turns bug fixes & feature requests into code changes (https://sweep.dev)
# For details on our config file, check out our docs at https://docs.sweep.dev

# If you use this be sure to frequently sync your default branch(main, master) to dev.
branch: 'main'
# By default Sweep will read the logs and outputs from your existing Github Actions. To disable this, set this to false.
gha_enabled: True
# This is the description of your project. It will be used by sweep when creating PRs. You can tell Sweep what's unique about your project, what frameworks you use, or anything else you want.
# Here's an example: sweepai/sweep is a python project. The main api endpoints are in sweepai/api.py. Write code that adheres to PEP8.
description: ''

# Default Values: https://github.com/sweepai/sweep/blob/main/sweep.yaml
```
</details>

Sweep uses our own Sandbox base image based this [Dockerfile](https://github.com/sweepai/sweep/blob/main/sweepai/sandbox/Dockerfile.sandbox), and first installs everything in the `install` section. Then on every edit, it runs the commands in the `check` section for the edited file. When it errors (non-zero return code), the logs get fed back into Sweep to fix the error, guaranteeing that every commit generates validated code.

By default we use Trunk, an opinionated super-linter that installs all the common formatters and linters for your codebase. You can set up and configure Trunk for yourself by following https://docs.trunk.io/get-started. This is equivalent to having the following `sandbox` config in your `sweep.yaml`:

<details open>
<summary>Default Sweep sandbox setup: Trunk</summary>

```yml
sandbox:
  install:
    - trunk init
  check:
    - trunk fmt {file_path}
    - trunk check --fix {file_path}
```
</details>

However, you can set up custom commands for your Sandbox. Here are some example `sweep.yaml` sandbox configurations:

<details open>
<summary>Python with&nbsp;`pylint`</summary>

Here's an example `sweep.yaml` for a Python repo that uses `pylint`.

```yml
sandbox:
  install:
    - pip install -r requirements.txt
  check:
    - pylint --errors-only {file_path}
```
</details>

<details>
<summary>Sweep's Own Config: Python 3.11, Poetry, and &nbsp;`pylint`</summary>

Sweep's own repo's `sweep.yaml` Python 3.11, Poetry and `pylint`.

```yml
sandbox:
  install:
    - apt install python3.11 -y
    - pip install poetry
    - poetry env use python3.11
    - poetry install
  check:
    - poetry run pylint --errors-only {file_path}
```
</details>

<details>
<summary>Sweep's landing page: Yarn with&nbsp;`prettier`,&nbsp;`eslint`&nbsp;and&nbsp;`tsc`</summary>

Our landing page, which is Typescript-based, we have the following config:

```yml
sandbox:
  install:
    - yarn install --ignore-engines
  check:
    - yarn run prettier --write {file_path}
    - yarn run eslint {file_path}
    - yarn run tsc
```
</details>

We recommend starting with the formatters, followed by the linters and type-checkers. This is because formatters give clear error messages for syntax errors.

## Testing

To ensure that your sandbox is set up properly, you can test it by creating an issue on a Sweep-installed repo with a title like

> Sweep (sandbox): FILE_PATH

where FILE_PATH is the path to the file you want to run the sandbox on. For example, we can test the sandbox on `sweepai/api.py` by titling the issue **Sweep (sandbox): sweepai/api.py**. You can find this at https://github.com/sweepai/sweep/issues/2371.
